package com.zqxq.lottery.controller.pay.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.DispatcherUtils;
import com.zqxq.lottery.config.AlipayConfig;
import com.zqxq.lottery.config.AlipayConfigDrawCash;
import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.controller.pay.IAlipayService;
import com.zqxq.lottery.entity.po.order.TradeInfoPO;
import com.zqxq.lottery.service.order.ITradeInfoService;
import com.zqxq.lottery.service.user.IUserFundsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Description ：
 * @Author ： 解风
 * @Date ： 2018/03/22 17:28
 */
@Service
@Slf4j
public class AlipayServiceImpl implements IAlipayService {

    @Autowired
    private IUserFundsService userFundsService;
    @Autowired
    private ITradeInfoService tradeInfoService;
    @Autowired
    private AlipayConfig alipayConfig;
    @Autowired
    private AlipayConfigDrawCash alipayConfigDrawCash;
    @Autowired
    private YmlConfig ymlConfig;

    @Override
    public Result saveTransferMoney(Long userId, Long cashId, BigDecimal money, String account) {
        AlipayClient alipayClient = getAlipayClientDrawCash();
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        JSONObject jsonObject = new JSONObject();
        if(cashId == null){
            cashId = ymlConfig.getPkId();
        }
        jsonObject.put("out_biz_no", cashId);
        jsonObject.put("payee_type", "ALIPAY_USERID");
        jsonObject.put("payee_account", account);
        jsonObject.put("amount", money.setScale(2,4).toString());
        jsonObject.put("payer_show_name", "天天趣玩");
//        jsonObject.put("payee_real_name", "重庆纵情向前科技有限公司");
        jsonObject.put("remark", "提现");
        String bizContent = jsonObject.toJSONString();
        request.setBizContent(bizContent);
        try{
            AlipayFundTransToaccountTransferResponse response = alipayClient.execute(request);
            //记录支付宝调用日志
            String transferMoney = response.getBody();
            log.error("transferMoney------------->:"+transferMoney);
            TradeInfoPO tradeInfoPO = new TradeInfoPO();
            tradeInfoPO.setUser_id(userId);
            tradeInfoPO.setTrade_no(response.getOrderId());
            tradeInfoPO.setTrade_way(1);
            tradeInfoPO.setTrade_type(3);
            tradeInfoPO.setOrder_id(cashId);
            tradeInfoPO.setAmount(money.setScale(2,4));
            tradeInfoPO.setResult(response.getMsg());
            tradeInfoPO.setData(transferMoney);
            tradeInfoPO.setCreate_time(DispatcherUtils.parseDateTime(response.getPayDate()));
            tradeInfoService.insert(tradeInfoPO);
            if(response.isSuccess()){
                System.out.println("调用成功");
                userFundsService.saveDrawCashSuccess(cashId);
            } else {
                userFundsService.saveDrawCashFail(userId,cashId, response.getSubMsg(), money);
                return new Result(10010, "网络繁忙，请稍后再试！");
            }
        }catch (Exception e) {
            log.error("transferMoney is error!", e);
            return new Result(CodeEnum.FAIL_100005.getCode(), CodeEnum.FAIL_100005.getMsg());
        }

        return new Result();
    }

    private AlipayClient getAlipayClient(){
        return new DefaultAlipayClient(alipayConfig.getServerUrl(), alipayConfig.getAppId(), alipayConfig.getPrivateKey(), "json", alipayConfig.getCharset(), alipayConfig.getPublicKey(), "RSA2");
    }

    private AlipayClient getAlipayClientDrawCash(){
        return new DefaultAlipayClient(alipayConfigDrawCash.getServerUrl(), alipayConfigDrawCash.getAppId(), alipayConfigDrawCash.getPrivateKey(), "json", alipayConfigDrawCash.getCharset(), alipayConfigDrawCash.getPublicKey(), "RSA2");
    }

}
